package com.bjtu.laorenyouhao.laoren.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bjtu.common.valid.group.AddGroupp;
import com.bjtu.common.valid.group.UpdateGroup;
import com.bjtu.laorenyouhao.laoren.entity.LaorenInfoEntity;
import com.bjtu.laorenyouhao.laoren.service.LaorenInfoService;
import com.bjtu.laorenyouhao.laoren.utils.HealthUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bjtu.laorenyouhao.laoren.entity.HealthEntity;
import com.bjtu.laorenyouhao.laoren.service.HealthService;
import com.bjtu.common.utils.PageUtils;
import com.bjtu.common.utils.R;



/**
 * 老人的健康信息
 *
 * @author yangyalong
 * @email yangyalong@gmail.com
 * @date 2021-07-08 20:27:26
 */
@RestController
@RequestMapping("laoren/health")
public class HealthController {
    @Autowired
    private HealthService healthService;

    @Autowired
    private LaorenInfoService laorenInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = healthService.queryPage(params);
        return R.ok().put("page", page);
    }

    @RequestMapping("emotion")
    public R emotion(@RequestBody Map data ){
//        {laorenName=王汉功, emotionType=5, date=2021-07-13 14:44:20}
        System.out.println(data);

        //根据名字查到老人
        LaorenInfoEntity one = laorenInfoService.getOne(new QueryWrapper<LaorenInfoEntity>().eq("name", data.get("laorenName")));


        //创建要插入数据库的health实体
        HealthEntity healthEntity = new HealthEntity();
        healthEntity.setEmotionType((Integer) data.get("emotionType"));
        healthEntity.setLaorenId(one.getId());
        healthEntity.setRecordTime(new Date());

        //查到老人分数
        List<HealthEntity> laorens = healthService.list(new QueryWrapper<HealthEntity>()
                .eq("laoren_id", one.getId())
                .orderByDesc("id"));

        //设置老人健康分数
        if (!laorens.isEmpty()){
            healthEntity.setHealthPoint(laorens.get(0).getHealthPoint() + HealthUtils.getPoints((Integer) data.get("emotionType")));
        } else {
            healthEntity.setHealthPoint(80);
        }

        healthService.save(healthEntity);
        System.out.println(healthEntity);
        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		HealthEntity health = healthService.getById(id);

        return R.ok().put("health", health);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@Validated(AddGroupp.class) @RequestBody HealthEntity health){
		healthService.save(health);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@Validated(UpdateGroup.class) @RequestBody HealthEntity health){
		healthService.updateById(health);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		healthService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
